<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>

            <a-col :span="12">
                <a-form-model-item label="仪表类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
                  <j-dict-select-tag type="list" v-model="model.type" dictCode="instrument_type" placeholder="请选择仪表类型" @change="yblxChange"/>
                </a-form-model-item>
            </a-col>

          <a-col :span="12">
            <a-form-model-item label="选择审批人" :labelCol="labelCol" :wrapperCol="wrapperCol"prop="approveUser">
              <j-select-multi-user :multiple='false' v-model="model.approveUser" />
            </a-form-model-item>
          </a-col>
          <a-spin :spinning="loading" style="width: 100%">
            <div v-for="(item, index) in insData" :key="index" >
              <a-col :span="6" >
              <a-form-model-item :label="item.name" :labelCol="{xs: { span: 24 },sm: { span:10 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 14 }}">
                <a-input-number v-model="model[item.id]" :defaultValue="0" placeholder="请输入表数" style="width: 100%" ></a-input-number>
              </a-form-model-item>
            </a-col>
              <a-col :span="4" >
                <a-form-model-item label="上次表数" :labelCol="{xs: { span: 24 },sm: { span:10 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 14 }}">
                  {{item.lastValue}}
                </a-form-model-item>
              </a-col>
              <a-col :span="2" >
                <a-form-model-item label="用量" :labelCol="{xs: { span: 24 },sm: { span:8 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 16 }}">
                  {{model[item.id]==null||undefined?0:model[item.id]-item.lastValue}}
                </a-form-model-item>
              </a-col>
              </div>
          </a-spin>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'UnPowerExportTempForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        insData:[],
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        loading:false,
        validatorRules: {
          // approveUser: [
          //   { required: true, message: '请选择审批人!'},
          // ],
          type: [
            { required: true, message: '请选择仪表类型!'},
          ],
        },
        url: {
          add: "/eneMon/unPowerExportTemp/mapadd",
          queryById: "/eneMon/unPowerExportTemp/queryById",
          list:"/eneMon/unPowerExportTemp/inslist"
        },
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      yblxChange(val){
        console.log(val)
        if(val==''){
          this.insData=[]
        }else {
          const _that = this
          let parm = {type: this.model.type, pageSize: -1}
          _that.loading = true
          getAction(this.url.list, parm).then((res) => {
            this.insData = res.result.records
          }).finally(() => {
            _that.loading = false
          })
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record,text) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }

            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>